﻿
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.Authority;
using S.Authorization.Domain.Model.User;
using S.Authorization.Domain.SqlServer;
using S.Authorization.Infrastructure.Interface.User;
using S.Common.Enums.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace S.Authorization.Infrastructure.Impl.User
{
    public class UserModellRepository : BaseRepository<UsersModel>, IUserModellRepository
    {
        public UserModellRepository(SqlDbContext db) : base(db)
        {
        }

        /// <summary>
        /// 人员管理显示
        /// </summary>
        /// <param name="userName">员工姓名</param>
        /// <param name="departmentId">所在部门</param>
        /// <param name="state">员工状态</param>
        /// <returns></returns>
        public async Task<List<GetUserListDto>> GetUserList(string userName, int departmentId, int state)
        {
            List<GetUserListDto> list=(from u in db.UsersModel
                                      join d in db.DepartmentModel
                                      on u.DepartmentId equals d.DepartMentId
                                      where u.IsDel==false && d.IsDel==true
                                      && ( string.IsNullOrEmpty(userName)||u.UserTrueName.Contains(userName)) && ( departmentId==0||u.DepartmentId==departmentId ) && (state==0 ||u.StaffState==(StaffStateEnum)state )
                                      select new GetUserListDto()
                                      {
                                          UserId=u.UserId,
                                          UserCode=u.UserCode,
                                          UserTrueName=u.UserTrueName,
                                          DepartmentId=u.DepartmentId,
                                          IndustryTitle=d.IndustryTitle,
                                          UserPhone = u.UserPhone,
                                          StaffState = u.StaffState
                                      }).ToList();
            return await Task.FromResult(list);
        }


        /// <summary>
        /// 添加用户并给用户设置角色
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <param name="roleId">角色</param>
        /// <returns></returns>
        public async Task<bool> AddUserAndRole(UsersModel user, int roleId)
        {
            var tran =db.Database.BeginTransaction();
            int isOk =0;
            try
            {
                db.UsersModel.AddAsync(user);
                isOk += await db.SaveChangesAsync();

                UserRole userRole = new UserRole()
                {
                    RoleId = roleId,
                    UserId = user.UserId,
                };
                db.UserRole.AddAsync(userRole);
                isOk += await db.SaveChangesAsync();

                tran.Commit();
                return isOk > 0;
            }
            catch (Exception)
            {
                tran.Rollback();
                throw;
            }
        }

    }
}
